package com.bdqn.controller;

import com.bdqn.entity.DataModel;
import com.bdqn.entity.Parameters;
import com.bdqn.entity.SaleGoods;
import com.bdqn.entity.SaleInventory;
import com.bdqn.service.SaleInventoryMapperService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin

public class SaleInventoryController {
    @Autowired
    SaleInventoryMapperService service;
    @RequestMapping(value = "getSaleInventory")
    public DataModel<SaleInventory> getSaleInventory(Parameters parameters){
        parameters.setPage((parameters.getPage()-1)*parameters.getLimit());
        DataModel<SaleInventory> dataModel=new DataModel<>();
        dataModel.setCode(0);
        dataModel.setMsg("");
        dataModel.setData(service.getSaleInventory(parameters));
        dataModel.setCount(service.getCount(parameters));
        return dataModel;
     }



    @RequestMapping(value = "InventoryExport")
    public boolean CreateTable(HttpServletResponse response, Parameters parameters) {
        parameters.setPage((parameters.getPage() - 1) * parameters.getLimit());
        try (Workbook workbook = new XSSFWorkbook();
//             FileOutputStream fileOut = new FileOutputStream("E:\\sale.xlsx")
             ServletOutputStream fileOut = response.getOutputStream();

        ) {
            response.setHeader("Content-Disposition", "attachment;fileName=sale_inventory.xls");
            Sheet sheet = workbook.createSheet("sale_goods");
            parameters.setLimit(999999999);
            List<SaleInventory> list = service.getSaleInventory(parameters);

            Row headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("序号");
            headerRow.createCell(1).setCellValue("商品名称");
            headerRow.createCell(2).setCellValue("进货日期");
            headerRow.createCell(3).setCellValue("最后销售日期");
            headerRow.createCell(4).setCellValue("进货数量");
            headerRow.createCell(5).setCellValue("销售数量");
            headerRow.createCell(6).setCellValue("库存数量");
            headerRow.createCell(7).setCellValue("进货总金额");
            headerRow.createCell(8).setCellValue("已销售总金额");


            int indexRow = 0;
            for (SaleInventory saleInventory : list) {
                Row row = sheet.createRow(++indexRow);
                row.createCell(0).setCellValue(indexRow);
                row.createCell(1).setCellValue(saleInventory.getCommodityName());
                row.createCell(2).setCellValue(saleInventory.getGet_date());
                row.createCell(3).setCellValue(saleInventory.getEnd_date());
                row.createCell(4).setCellValue(saleInventory.getGet_num());
                row.createCell(5).setCellValue(saleInventory.getSale_num());
                row.createCell(6).setCellValue(saleInventory.getInventory_num());
                row.createCell(7).setCellValue(saleInventory.getGet_totalPrice());
                row.createCell(8).setCellValue(saleInventory.getSale_totalPrice());
            }
            workbook.write(fileOut); // 写入文件


        }catch (IOException e){
            e.printStackTrace();
            return false;
        }
        return true;
    }


}
